﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Extensions;

public partial class Controls_PageContentHeader : System.Web.UI.UserControl
{
    private int _catId;
    protected void Page_Load(object sender, EventArgs e)
    {
        _catId = Request.QueryString["id"].ToInt32();
        using (DAL.EShopDBDataContext db = new DAL.EShopDBDataContext())
        {
            var cat = db.Categories.Single(c => c.CategoryId == _catId);
            CategoryTitleLabel.Text = cat.Title;
            CategoryDescription.Text = cat.Description;
        }
    }

    protected void DescriptionEditor_Save(object sender, Winthusiasm.HtmlEditor.HtmlEditor.SaveEventArgs e)
    {
        if (!Roles.IsUserInRole("Administrator"))
            return;
        CategoryDescription.Text = DescriptionEditor.Text;
        DAL.Category cat = new DAL.Category() { CategoryId = _catId, Description = DescriptionEditor.Text };
        cat.Update();
    }
    protected virtual void UpdateCategoryTitle()
    {
        if (!Roles.IsUserInRole("Administrator"))
            return;
        CategoryTitleLabel.Text = CategoryTitleTextBox.Text;
        DAL.Category cat = new DAL.Category { CategoryId = _catId, Title = CategoryTitleTextBox.Text };
        cat.Update();
        SiteMap.CurrentNode.Title = CategoryTitleTextBox.Text;
    }
    protected void TitleEditButton_Click(object sender, ImageClickEventArgs e)
    {
        if (!Roles.IsUserInRole("Administrator"))
            return;
        CategoryDescription.Visible = !CategoryDescription.Visible;
        DescriptionEditor.Visible = !CategoryDescription.Visible;
        if (DescriptionEditor.Visible)
            DescriptionEditor.Text = CategoryDescription.Text;
        CategoryTitleLabel.Visible = !CategoryTitleLabel.Visible;
        CategoryTitleTextBox.Visible = !CategoryTitleLabel.Visible;
        if (CategoryTitleTextBox.Visible)
            CategoryTitleTextBox.Text = SiteMap.CurrentNode.Title;
        else
            UpdateCategoryTitle();
    }
}
